package com.utils;

import com.alibaba.fastjson.JSONObject;
import com.common.GmallConfig;
import redis.clients.jedis.Jedis;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import static com.utils.JdbcUtils.queryList;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2022/1/6 9:26
 */
public class DimUtil {
    public static JSONObject getDimInfo(Connection connection, String tableName, String id) throws InvocationTargetException, SQLException, InstantiationException, IllegalAccessException {

        //查询Phoenix之前先查询 Redis
        Jedis jedis = RedisUtil.getJedis();

        //DIM:DIM_USER_INFO:666
        String redisKey = "DIM:" + tableName + ":" + id;

        String dimInfoJsonStr = jedis.get(redisKey);

        if(dimInfoJsonStr!=null||"".equals(dimInfoJsonStr)){

            //重置过期时间
            jedis.expire(redisKey,24*60*60);

            //归还连接
            jedis.close();

            return JSONObject.parseObject(dimInfoJsonStr);

        }


        //拼接查询语句
        // select * from db.tn where id ='666'
        StringBuffer querySql = new StringBuffer();
        querySql.append("select * from ")
                .append(GmallConfig.HBASE_SCHEMA)
                .append(".")
                .append(tableName)
                .append(" where id = '")
                .append(id)
                .append("'");

        //查询Phoenix
        List<JSONObject> queryList = queryList(connection, querySql.toString(), JSONObject.class, false);

        JSONObject dimInfoJson = queryList.get(0);

        //将返回结果之前，将数据写入Redis
        jedis.set(redisKey,dimInfoJson.toJSONString());
        jedis.expire(redisKey,24*60*60);
        jedis.close();

        return dimInfoJson;
    }

    public static void delRedisDimInfo(String tableName,String id){
        Jedis jedis = RedisUtil.getJedis();

        //DIM:DIM_USER_INFO:666
        String redisKey = "DIM:" + tableName + ":" + id;

        jedis.del(redisKey);


        jedis.close();

    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, InvocationTargetException {


        Class.forName(GmallConfig.PHOENIX_DRIVER);
        Connection connection = DriverManager.getConnection(GmallConfig.PHOENIX_SERVER);

        long start = System.currentTimeMillis();
        JSONObject result = getDimInfo(connection, "Z_TEST_FILTER", "8");
        long end = System.currentTimeMillis();

        long start1 = System.currentTimeMillis();
        JSONObject result1 = getDimInfo(connection, "DIM_USER_INFO", "797");
        long end1 = System.currentTimeMillis();

        long start2 = System.currentTimeMillis();
        JSONObject result2 = getDimInfo(connection, "DIM_USER_INFO", "797");
        long end2 = System.currentTimeMillis();

        System.out.println(result.toJSONString());
        System.out.println(end - start);

        System.out.println(result1.toJSONString());
        System.out.println(end1 - start1);

        System.out.println(result2.toJSONString());
        System.out.println(end2 - start2);

        connection.close();
    }
}
